package com.example.test_gen.fastorm;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Stream;

public class GenProxy {
    static HashMap<String, String> tableNameMap = new HashMap<String, String>() {{
        put("Goods", "erp_goods");
        put("GoodsModel", "erp_goods_model");
    }};
    static String packageName = "com.example.test_gen";
    static String rootDir = "E:\\code\\test_gen\\src\\main\\java\\com\\example\\test_gen";

    public static String toUnderScoreCase(String camelCaseStr) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < camelCaseStr.length(); i++) {
            char c = camelCaseStr.charAt(i);
            if (Character.isUpperCase(c)) {
                if (i > 0) {
                    builder.append('_');
                }
                builder.append(Character.toLowerCase(c));
            } else {
                builder.append(c);
            }
        }
        return builder.toString();
    }

    protected static void genItem(String pojoPath, String proxyPath) {
        try {
            List<String> contentArr = new ArrayList<>();

            File file = new File(pojoPath);
            ParserConfiguration parserConfiguration = new ParserConfiguration();
            JavaParser javaParser = new JavaParser(parserConfiguration);
            ParseResult<CompilationUnit> compilationUnit = javaParser.parse(file);
            List<ClassOrInterfaceDeclaration> classDeclarations = compilationUnit.getResult().get().findAll(ClassOrInterfaceDeclaration.class);

            for (ClassOrInterfaceDeclaration classDeclaration : classDeclarations) {
                String entityName = classDeclaration.getName().toString();

                contentArr.add("package " + packageName + ".proxy;");
                contentArr.add("");
                contentArr.add("import " + packageName + ".fastorm.BaseProxy;");
                contentArr.add("import " + packageName + ".fastorm.TableField;");
                contentArr.add("import org.springframework.stereotype.Component;");
                contentArr.add("");
                contentArr.add("@Component");
                contentArr.add("public class " + entityName + "Proxy implements BaseProxy {");

                for (FieldDeclaration field : classDeclaration.getFields()) {
                    String type = field.getElementType().asString();
                    String fieldName = field.getChildNodes().get(1).toString();

                    contentArr.add("    public TableField " + fieldName + " = new TableField(\"" + toUnderScoreCase(fieldName) + "\");");
                    contentArr.add("");
                }

                contentArr.add("    @Override");
                if (tableNameMap.containsKey(entityName)) {
                    contentArr.add("    public String getTableName() {return \"" + tableNameMap.get(entityName) + "\";}");
                }else{
                    contentArr.add("    public String getTableName() {return \"\";}");
                }

                contentArr.add("}");
            }

            FileUtil.writeTextToFile(String.join("\n", contentArr), proxyPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        List<String> fromPathList = new ArrayList<>();
        fromPathList.add(rootDir + "\\po\\");
        fromPathList.add(rootDir + "\\vo\\statistics\\");

        for (String fromPath : fromPathList) {
            Path dirPath = Paths.get(fromPath);
            try (Stream<Path> paths = Files.walk(dirPath)) {
                paths.filter(Files::isRegularFile).forEach(pojoPath -> {
                    String pojoPathStr = pojoPath.toString();

                    String[] pathArr = pojoPathStr.split("\\\\");
                    String className = pathArr[pathArr.length - 1].replace(".java", "");
                    String proxyPathStr = rootDir + "\\proxy\\" + className + "Proxy.java";

                    genItem(pojoPathStr, proxyPathStr);
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
